package aStar;


public class Field {
	
	private Tile[][] field;

	public Field() {
		field = new Tile[Tester.FIELD_WIDTH][Tester.FIELD_HEIGHT];
		for(int i=0;i < Tester.FIELD_WIDTH;i++){
			for(int j=0;j < Tester.FIELD_HEIGHT;j++){
				field[i][j] = new Tile(i, j, 1);
			}
		}
	}
	
	//Returns the entire field
	public Tile[][] getField(){
		return field;
	}
	
	//Getter for a specific tile
	public Tile getTile(int tileX, int tileY) {
		return field[tileX][tileY]; 
	}
	
	//Setter for the height of a tile
	public void setTileHeight(int tileX, int tileY, int height) {
		field[tileX][tileY].tileHight = height;
	}
	
	//elevates a group of tiles around a specific tile
	public void elevateTiles(int centerTileX, int centerTileY, int elevation, int radius){
		for(int i=centerTileX-radius; i<centerTileX+radius+1;i++) {
			for(int j=centerTileY-radius; j<centerTileY+radius+1;j++) {
				if (tileExists(i, j)) {
					setTileHeight(i, j,(field[i][j].tileHight + elevation));
				}
			}
		}
	}
	
	//check if a tile exists
	public boolean tileExists(int tileX, int tileY) {
		if (tileX < 0 || tileY < 0)
			return false;
		if (tileX >= field.length || tileY >=field[0].length)
			return false;
		
		return true;
	}
	
	public void cleanField() {
		for(Tile[] colum : field){
			for(Tile tile : colum){
				tile.F = tile.H = tile.G = 0;
				tile.setParent(tile);
			}
		}
	}
	
	//Dump the field setup in console
	public void exportField() {
		for(Tile[] colum : getField()) {
			for(Tile tile : colum) {
				System.out.print("data.setTileHeight(" + tile.tileX + ", "+tile.tileY+", "+tile.tileHight+");");
			}
			System.out.println();
		}
	}
}

